<?php
declare(strict_types = 1);

namespace leruge\command;

use think\console\Command;

class Socket extends Command
{
    public function configure()
    {
        $this->setName('socket')
            ->addArgument('action', \think\console\input\Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
            ->setDescription('linux下启动socket');
    }

    public function handle()
    {
        $action = $this->input->getArgument('action');
        if (!in_array($action, ['start', 'stop', 'reload', 'restart', 'status', 'connections'])) {
            $this->output->error('无效的命令[' . $action . ']');
            exit(1);
        }
        $actionArray = [
            'start' => 'socket启动',
            'stop' => 'socket关闭',
            'reload' => 'socket平滑重启',
            'restart' => 'restart',
            'status' => '查看socket状态',
            'connections' => '查看链接'
        ];
        $tip = $actionArray[$action] ?? null;
        if ($tip) {
            $this->output->info($tip);
        }
        $registerAddress = config('chat.register_address') ?: '127.0.0.1:1201';
        // register注册
        $this->register($registerAddress);

        // business启动
        $this->business($registerAddress);

        // gateway启动
        $this->gateway($registerAddress);

        \Workerman\Worker::$logFile = config('chat.log_file');
        \Workerman\Worker::$daemonize = config('chat.daemonize');
        \Workerman\Worker::runAll();;
    }

    // register注册
    private function register($registerAddress)
    {
        $register = new \GatewayWorker\Register('text://' . $registerAddress);
        $register->name = config('chat.register_name') ?: 'register';
    }

    // business启动
    private function business($registerAddress)
    {
        $worker = new \GatewayWorker\BusinessWorker();
        $worker->registerAddress = $registerAddress;
        $worker->name = config('chat.business_name') ?: 'business';
        $worker->count = 1;
        $worker->eventHandler = config('chat.event_handler') ?: \leruge\Events::class;
    }

    // gateway启动
    private function gateway($registerAddress)
    {
        $gatewayAddress = config('chat.gateway_address') ?: 'websocket://0.0.0.0:8201';
        $content = [];
        if (config('chat.ssl.is_use')) {
            $content = [
                'ssl' => config('chat.ssl')
            ];
        }
        $gateway = new \GatewayWorker\Gateway($gatewayAddress, $content);
        if (config('chat.ssl.is_use')) {
            $gateway->transport = 'ssl';
        }
        $gateway->name = config('chat.gateway_name') ?: 'gateway';
        $gateway->count = config('chat.gateway_count') ?: 1;
        $gateway->lanIp = config('chat.gateway_lan_ip') ?: '127.0.0.1';
        $gateway->startPort = config('chat.gateway_start_port') ?: 4001;
        $gateway->pingInterval = config('gateway.gateway_ping_interval') ?: 55;
        $gateway->pingNotResponseLimit = 0;
        $gateway->pingData = '{"type":"ping"}';
        $gateway->registerAddress = $registerAddress;
    }
}